package com.example.my.agiftthat.ui.my;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.my.agiftthat.MvpActivity.ActivityCar;
import com.example.my.agiftthat.R;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bmob.newsmssdk.BmobSMS;
import cn.bmob.newsmssdk.exception.BmobException;
import cn.bmob.newsmssdk.listener.RequestSMSCodeListener;
import cn.bmob.newsmssdk.listener.VerifySMSCodeListener;

/**
 * Created by My on 2016/12/3.
 */

public class FragmentMy extends Fragment {
    @Bind(R.id.text_click)
    TextView textClick;
    @Bind(R.id.button)
    Button button;
    @Bind(R.id.text1)
    TextView text1;
    @Bind(R.id.editTextss)
    EditText editTextss;
    @Bind(R.id.editTexts)
    EditText editTexts;
    @Bind(R.id.image_clear)
    ImageView imageClear;
    private String string1;
    private String string2;
    private View view;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BmobSMS.initialize(getActivity(), "2a060f5f1c2a836e707bcd6c693a199d");
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.framgent_my, container, false);
        ButterKnife.bind(this, view);
        return view;

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //获取短信的方法
        onClick();
        //扫描二维码的方法
        onClear();

    }

    private void onClear() {
        imageClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }

    private void onClick() {


        text1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                string1 = editTextss.getText().toString();

                Log.i("aaaa", "路过1" + string2 + string1);
                if (string1.length() == 11) {
                    BmobSMS.requestSMSCode(getActivity(), string1, "短信模板", new RequestSMSCodeListener() {
                        @Override
                        public void done(Integer integer, BmobException e) {
                            if (e == null) {
                                //设置按钮不可点击
                                text1.setClickable(false);
                                text1.setBackgroundColor(Color.GRAY);
                                new CountDownTimer(60000, 1000) {
                                    @Override
                                    public void onTick(long millisUntilFinished) {
                                        String time = millisUntilFinished / 1000 + "s秒";
                                        text1.setText(time + "后重新发送");
                                    }

                                    @Override
                                    public void onFinish() {
                                        text1.setClickable(true);
                                        text1.setBackgroundColor(Color.RED);
                                        text1.setText("获取验证码");
                                    }
                                }.start();
                            } else {
                                Toast.makeText(getActivity(), "请输入正确的手机号" + string1, Toast.LENGTH_SHORT).show();

                            }
                        }
                    });
                } else {
                    Toast.makeText(getActivity(), "请输入11位有效手机号码" + string1, Toast.LENGTH_SHORT).show();
                }
            }
        });
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                string2 = editTexts.getText().toString();
                BmobSMS.verifySmsCode(getActivity(), string1, string2, new VerifySMSCodeListener() {
                    @Override
                    public void done(BmobException e) {
                        if (e == null) {
                            Intent intent = new Intent(getActivity(), ActivityCar.class);
                            getActivity().startActivity(intent);
                        } else {
                            Toast.makeText(getActivity(), "验证码错误", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
